package controller

import (
	"ais-go/app/models"
	"ais-go/app/tools"
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
	"strconv"
)

type SystemCtl struct {
	db *gorm.DB
}

var System = SystemCtl{}

// GetUserList 用户的增删改查
func (ctl *SystemCtl) GetUserList(c *gin.Context) {
	ctl.db = models.Db
	var userList []models.User
	ctl.db.Preload("Role.Right").Find(&userList)
	tools.SuccessResp(c, "获取成功", userList)
}

func (ctl *SystemCtl) AddUser(c *gin.Context) {
	ctl.db = models.Db
	var user models.User
	c.ShouldBind(&user)
	ctl.db.Create(&user)
	tools.SuccessResp(c, "添加成功", nil)
}

func (ctl *SystemCtl) UpdateUser(c *gin.Context) {
	ctl.db = models.Db
	var user models.User
	c.ShouldBind(&user)
	ctl.db.Model(&user).Association("Role").Replace(user.Role)
	ctl.db.Save(&user)
	tools.SuccessResp(c, "修改成功", nil)
}

func (ctl *SystemCtl) DelUser(c *gin.Context) {
	ctl.db = models.Db
	var user models.User
	c.ShouldBind(&user)
	ctl.db.Select("Role").Delete(&user)
	tools.SuccessResp(c, "删除成功", nil)
}

func (ctl *SystemCtl) GetRoleList(c *gin.Context) {
	ctl.db = models.Db
	var roleList []models.Role
	ctl.db.Preload("Right").Find(&roleList)
	tools.SuccessResp(c, "获取成功", roleList)
}

func (ctl *SystemCtl) AddRole(c *gin.Context) {
	ctl.db = models.Db
	var role models.Role
	c.ShouldBind(&role)
	ctl.db.Create(&role)
	tools.SuccessResp(c, "添加成功", nil)
}

func (ctl *SystemCtl) UpdateRole(c *gin.Context) {
	var err error
	ctl.db = models.Db
	var role models.Role
	c.ShouldBind(&role)
	//ctl.db.Model(&role).Association("Right").Replace(role.Right)
	//ctl.db.Updates(&role)
	err = ctl.db.Transaction(func(tx *gorm.DB) error {
		if err = tx.Updates(&role).Error; err != nil {
			return err
		}
		if err = tx.Model(&role).Association("Right").Replace(role.Right); err != nil {
			return err
		}
		return nil
	})
	if err != nil {
		tools.ErrorResp(c, 500, "修改失败")
		return
	}
	tools.SuccessResp(c, "修改成功", nil)
}

func (ctl *SystemCtl) DelRole(c *gin.Context) {
	var err error
	ctl.db = models.Db
	var role models.Role
	c.ShouldBind(&role)
	//ctl.db.Select("Right").Delete(&role)
	err = ctl.db.Select("Right").Delete(&role).Error
	if err != nil {
		tools.ErrorResp(c, 500, "删除失败")
		return
	}
	tools.SuccessResp(c, "删除成功", nil)
}

func (ctl *SystemCtl) GetRightList(c *gin.Context) {
	ctl.db = models.Db
	var rightList []models.Right
	ctl.db.Find(&rightList)
	tools.SuccessResp(c, "获取成功", rightList)
}

func (ctl *SystemCtl) AddRight(c *gin.Context) {
	ctl.db = models.Db
	var right models.Right
	c.ShouldBind(&right)
	ctl.db.Create(&right)
	tools.SuccessResp(c, "添加成功", nil)
}

func (ctl *SystemCtl) UpdateRight(c *gin.Context) {
	ctl.db = models.Db
	var right models.Right
	c.ShouldBind(&right)
	ctl.db.Save(&right)
	tools.SuccessResp(c, "修改成功", nil)
}

func (ctl *SystemCtl) DelRight(c *gin.Context) {
	var err error
	ctl.db = models.Db
	var right models.Right
	c.ShouldBind(&right)
	err = ctl.db.Delete(&right).Error
	if err != nil {
		tools.ErrorResp(c, 500, "删除失败")
		return
	}
	tools.SuccessResp(c, "删除成功", nil)
}

func (ctl *SystemCtl) GetUser(c *gin.Context) {
	ctl.db = models.Db
	//cookie, _ := c.Cookie("userId")
	cookie := c.Query("userId")
	key := "91234567890123456789012345678901"
	decryptCookie := tools.AesDecrypt(cookie, key)
	//转为uint
	decryptCookieInt, _ := strconv.ParseUint(decryptCookie, 10, 64)
	var user models.User
	ctl.db.Preload("Role.Right").First(&user, decryptCookieInt)
	if user.Username == "" {
		tools.ErrorResp(c, 500, "用户不存在")
		return
	}
	tools.SuccessResp(c, "查询成功", user)
}
